/*
Copyright (C) 2022-2024 Inspur Corp. All Rights Reserved.

SPDX-License-Identifier: Apache-2.0
*/
package service

import (
	"icep/bean/entity"
	"icep/bean/model"
	"icep/common/constant"
)

func (is *IcepService) GetUserByAccount(account string) (*model.UserDetail, error) {
	user := make([]*entity.IcepUser, 0)
	err := is.DbEngine.Where("phone = ? or mail = ?", account, account).Find(&user)
	if err != nil {
		return nil, err
	}
	if len(user) == 0 {
		return nil, nil
	}

	userDetail := &model.UserDetail{
		Id:          user[0].Id,
		TenantId:    user[0].TenantId,
		UserType:    user[0].UserType,
		Phone:       user[0].Phone,
		Mail:        user[0].Mail,
		Password:    user[0].Password,
		Status:      user[0].Status,
		CreatedTime: user[0].CreatedTime.Format(constant.TimeLayout),
		UpdatedTime: user[0].UpdatedTime.Format(constant.TimeLayout),
	}

	return userDetail, nil
}
